
//camera.cpp
#include "camera.h"

Camera::Camera(void)
{
	cameraType = AIRCRAFT;
	
	pos.x = 0.0f;
	pos.y = 0.0f;
	pos.z = 0.0f;

	look.x = 0.0f;
	look.y = 0.0f;
	look.z = 1.0f;

	up.x = 0.0f;
	up.y = 1.0f;
	up.z = 0.0f;

	right.x = 1.0f;
	right.y = 0.0f;
	right.z = 0.0f;

}
Camera::Camera(CameraType cType)
{
	cameraType = cType;
	pos.x = 0.0f;
	pos.y = 0.0f;
	pos.z = 0.0f;

	look.x = 0.0f;
	look.y = 0.0f;
	look.z = 1.0f;

	up.x = 0.0f;
	up.y = 1.0f;
	up.z = 0.0f;

	right.x = 1.0f;
	right.y = 0.0f;
	right.z = 0.0f;

}
Camera::~Camera()
{
}



void Camera::strafe(float units)
{
	// move only on xZ plane for land objects
	if( cameraType == LANDOBJECT)
		pos += D3DXVECTOR3(right.x, 0.0f, right.z) * units;
	if(cameraType == AIRCRAFT)
		pos += right * units;
}
void Camera::fly(float units)
{
	if(cameraType == AIRCRAFT)
		pos += up * units;
}
void Camera::walk(float units)
{
	// move only on xy plane for land objects
	if( cameraType == LANDOBJECT)
		pos += D3DXVECTOR3(look.x, 0.0f, look.z) * units;
	if(cameraType == AIRCRAFT)
		pos += look * units;
}
void Camera::pitch(float angle)
{
	D3DXMATRIX T;
	D3DXMatrixRotationAxis(&T, &right, angle);
	// rotate up and look around right vector
	D3DXVec3TransformCoord(&up, &up, &T);
	D3DXVec3TransformCoord(&look, &look, &T);
}
void Camera::yaw(float angle)
{
	D3DXMATRIX T;
	//rotate around world y (0,1,0) always for land objects
	if( cameraType == LANDOBJECT)
		D3DXMatrixRotationY(&T, angle);
	//rotate around own up vector for aircraft
	if( cameraType == AIRCRAFT)
		D3DXMatrixRotationAxis(&T, &up, angle);

	// rotate right and look around up or y-axis
	D3DXVec3TransformCoord(&right, &right, &T);
	D3DXVec3TransformCoord(&look, &look, &T);
}
void Camera::roll(float angle)
{
	if( cameraType == AIRCRAFT)
	{
		D3DXMATRIX T;
		D3DXMatrixRotationAxis(&T, &look, angle);

		// rotate up and right around look vector
	D3DXVec3TransformCoord(&right, &right, &T);
	D3DXVec3TransformCoord(&up, &up, &T);
	}
}
void Camera::getViewMatrix(D3DMATRIX* V)
{
	//Keep camera's axes orthogonal to each other
	D3DXVec3Normalize(& look, & look);
	
	D3DXVec3Cross(&right, &up, &look);
	D3DXVec3Normalize(&right, &right);

	D3DXVec3Cross(&up, &look, &right);
	D3DXVec3Normalize(&up, &up);

	//Build the Veiw Matrix
	float x = -D3DXVec3Dot(& right, & pos);
	float y = -D3DXVec3Dot(& up, & pos);
	float z = -D3DXVec3Dot(& look, & pos);

	V->_11 = right.x;
	V->_12 = up.x;
	V->_13 = look.x;
	V->_14 = 0.0f;
 
	V->_21 = right.y;
	V->_22 = up.y;
	V->_23 = look.y;
	V->_24 = 0.0f;

	V->_31 = right.z;
	V->_32 = up.z;
	V->_33 = look.z;
	V->_34 = 0.0f;

	V->_41 = x;
	V->_42 = y;
	V->_43 = z;
	V->_44 = 1.0f;


}
void Camera::setCameraType(CameraType cType)
{
}
void Camera::getPosition(D3DXVECTOR3* pos)
{
}
void Camera::setPosition(D3DXVECTOR3* pos)
{
	this->pos.x = pos->x;
	this->pos.y = pos->y;
	this->pos.z = pos->z;
}
void Camera::getRight(D3DXVECTOR3* pos)
{
}
void Camera::getUp(D3DXVECTOR3* pos)
{
}
void Camera::getLook(D3DXVECTOR3* pos)
{
}